package com.job.servicejob.controller;


import com.job.commonutils.JwtUtils;
import com.job.commonutils.R;
import com.job.servicebase.exceptionHandler.JobException;
import com.job.servicejob.entity.Admin;
import com.job.servicejob.entity.vo.RegisterVo;
import com.job.servicejob.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author myt
 * @since 2023-03-17
 */

@RestController
@RequestMapping("/usercenter/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody Admin admin){

        //调用service方法实现登录
        String token = adminService.login(admin);
        //返回token值
        return R.ok().data("token",token);
    }
//根据手机号查id
    @GetMapping("getId/{phone}")
    public  R getId(@PathVariable String phone){
//    Admin stu = adminService.getById(phone);
        List<Admin> stu = adminService.getByPhone(phone);

        return R.ok().data("stu",stu);
    }

    //logout
    @PostMapping("logout")
    public R logout(){
        return R.ok();
    }
    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        adminService.register(registerVo);
        return R.ok();
    }
    //修改密码
    @PostMapping("updatePassword")
    public R updateJob (@RequestBody Admin admin){
        boolean flag = adminService.updateById(admin);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //根据token获取用户信息
//    @ApiOperation(value = "根据token获取登录信息")
    @PostMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
//          System.out.println(33333);
//        System.out.println(request);
//        System.out.println(4444444);

        String userId = JwtUtils.getPhoneAdminByJwtToken(request);
//        System.out.println(66666);
//        System.out.println(userId);
//        System.out.println(77777);


        Admin user = adminService.getById(userId);
//        System.out.println(88888);
//        System.out.println(user);
//        System.out.println(99999);

        return R.ok().data("userInfo",user);
//
    }

}

